/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main(){
    pid_t pid = fork();
    pid_t pr;
    if(pid < 0){
        perror("fork error\n");
        return 0;
    }
    else if(pid > 0){
        printf("Parent process\n");
        do{
            pr = waitpid(pid, NULL, WNOHANG);
            if(pr == 0){
                printf("No child exited!\n");
                sleep(1);
            }
        }while(pr == 0);
        if(pr == pid){
            printf("successfully get child %d\n", pr);
        }
        else{
            printf("some error occured!\n");
        }
    }
    else if(pid == 0){
        printf("Sub process, PID: %u, PPID: %u\n", getpid(), getppid());
        sleep(10);
        exit(0);
    }
    return 0;
}